const { defineConfig } = require('@vue/cli-service')
const bodyParser = require('body-parser')
const Mock = require('mockjs')
// 模拟数据

const data = Mock.mock({
  'list|30': [
    {
      'id|+1': 1,
      provice: '@province()',
      'children|15': [
        {
          id: '@id()',
          city: '@city()',
        },
      ],
    },
  ],
})

// 这个文件，一更新，就要重新启动
module.exports = defineConfig({
  transpileDependencies: true,
  devServer: {
    setupMiddlewares: (middlewares, { app }) => {
      app.use(bodyParser.json())
      app.get('/api/get-test', (req, res) => {
        res.send({
          code: 0,
          msg: 'success',
          data: { query: req.query },
        })
      })

      app.get('/api/list', (req, res) => {
        res.send({
          code: 0,
          msg: 'success',
          data: data.list,
        })
      })

      app.post('/api/post-test', (req, res) => {
        res.send({
          code: 0,
          msg: 'success',
          data: { body: req.body },
        })
      })
      return middlewares
    },
  },
})
